﻿using System;
using System.ComponentModel;

using RegexEditor.Properties;
using System.Windows.Forms;

namespace RegexEditor
{
	public partial class PreferencesForm : BaseForm
	{
		public PreferencesForm(MainForm parent)
		{
			InitializeComponent();
		}

		protected override void OnLoad(EventArgs e)
		{
			historySizeTextBox.Value = Settings.Default.SaveHistorySize;
			liveUpdateThresholdTextBox.Value = Settings.Default.MaximumMatchTreshold;

			base.OnLoad(e);
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			Settings.Default.SaveHistorySize = Convert.ToInt32(historySizeTextBox.Value);
			Settings.Default.MaximumMatchTreshold = Convert.ToInt32(liveUpdateThresholdTextBox.Value);

			base.OnClosing(e);
		}

		private void clearSavedButton_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("Are you sure you want to clear all currently saved expressions?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
			{
				SavedExpression.ClearAll();
				MessageBox.Show("Saved expressions have been removed.", "Action completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		private void resetSettingsButton_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("Are you sure you want to reset all settings to factory defaults?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
			{
				string savedExpressions = Settings.Default.SavedExpressions;
				Settings.Default.Reset();
				Settings.Default.SavedExpressions = savedExpressions;
				OnLoad(EventArgs.Empty);
				Settings.Default.Save();
				MessageBox.Show("Settings have been restored to factory defaults.", "Action completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
				Close();
			}
		}
	}
}
